import {createSlice} from '@reduxjs/toolkit'
import api from '@/api'
import {tranformMenu,transfromRoute} from '@/utils/authUtils'

import withLoadable from '@/hoc/loadable'
import Auth from "@/component/Auth"
const Login=withLoadable(()=>import('@/views/Login'))
const Home=withLoadable(()=>import('@/views/Home'))
const authSlice=createSlice({
    name:'authNS',
    initialState:{
        menus:[],
        routes:[]
    },
    reducers:{
        getMenus(state,action){
            state.menus=action.payload
        },
        getRoutes(state,action){
            state.routes=action.payload
        }
    }
})

export const getMenusAsync=async(dispatch)=>{
    const result=await api.login.getMenusApi()
    const ary=tranformMenu(result.data)
    dispatch(getMenus(ary))
}
export const getRoutesAsync=async(dispatch)=>{
    const result=await api.login.getMenusApi()
    const ary=transfromRoute(result.data)
    const routes=[
        {
          path:'/login',
          element:<Login/>
        },
        {
          path:'/',
          element:<Auth><Home/></Auth>
        }
      ]
      routes[1].children=ary
      dispatch(getRoutes(routes))
    

}
export const {getMenus,getRoutes}=authSlice.actions
export default authSlice.reducer
